//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    uid: "",
    orderId:"",
    orderInfo:[],
    imgList: [],
    // 
    orderNumber:"",
    degreeId:"",
    degreeList: [{
        icon: "iconfont icon-haoping1",
        words: "好评",
        id: "0",
        active: false,
      }, {
        icon: "iconfont icon-zhongping",
        words: "中评",
        id: "1",
        active: false,
      },
      {
        icon: "iconfont icon-chaping",
        words: "差评",
        id: "2",
        active: false,
      },
    ],
    // 
    priceDes: "",
    priceId: "",
    priceStar: [{
        id: "1",
        active: false,
        des: "非常差",
      },
      {
        id: "2",
        active: false,
        des: "差",
      },
      {
        id: "3",
        active: false,
        des: "一般",
      },
      {
        id: "4",
        active: false,
        des: "好",
      },
      {
        id: "5",
        active: false,
        des: "非常好",
      },
    ],
    // 
    serviceDes: "",
    serviceId: "",
    serviceStar: [{
        id: "1",
        active: false,
        des: "非常差",
      },
      {
        id: "2",
        active: false,
        des: "差",
      },
      {
        id: "3",
        active: false,
        des: "一般",
      },
      {
        id: "4",
        active: false,
        des: "好",
      },
      {
        id: "5",
        active: false,
        des: "非常好",
      },
    ], // 
    qualityDes: "",
    qualityId: "",
    qualityStar: [{
        id: "1",
        active: false,
        des: "非常差",
      },
      {
        id: "2",
        active: false,
        des: "差",
      },
      {
        id: "3",
        active: false,
        des: "一般",
      },
      {
        id: "4",
        active: false,
        des: "好",
      },
      {
        id: "5",
        active: false,
        des: "非常好",
      },
    ],
    // 评价内容
    content: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      orderId: options.orderId,
    })
    this.orderDetail();
  },

  // delTap
  delTap: function(e) {
    var imgList = this.data.imgList;
    imgList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: imgList,
    })
  },
  // chooseImg
  chooseImg: function(e) {
    var that = this;
    var base64 = this.data.base64;
    var imgSrc = this.data.imgSrc;
    var imgUrl = this.data.imgUrl;
    var imgList = this.data.imgList;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // 可以指定是原图还是压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        wx.request({
          url: tempFilePaths,
          method: 'GET',
          responseType: 'arraybuffer',
          success: function(res) {
            base64 = wx.arrayBufferToBase64(res.data);
            that.setData({
              base64: base64,
              imgSrc: 'data:image/jpg;base64,' + base64
            })
            // console.log(base64)
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=LoadPic"),
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'post',
              data: baseJs.json2Form({
                HeadImage: base64,
              }),
              success: function(res) {
                console.log(res);
                imgList.push(res.data.ResultData);
                that.setData({
                  imgList: imgList,
                })
              }
            })
          }
        })
      }
    })
  },
  // 好评、中评、差评
  degreeTap: function(e) {
    console.log(e);
    var degreeList = this.data.degreeList;
    var degreeId = this.data.degreeId;
    degreeId = e.currentTarget.dataset.degreeid;
    for (var i = 0; i < degreeList.length; i++) {
      if (degreeList[i].id == degreeId) {
        degreeList[degreeId].active = true;
      } else {
        degreeList[i].active = false;
      }
    }
    this.setData({
      degreeList: degreeList,
      degreeId: degreeId,
    })
  },
  // 
  priceStarTap: function(e) {
    var priceStar = this.data.priceStar;
    var priceId = this.data.priceId;
    var priceDes = this.data.priceDes;
    priceId = e.currentTarget.dataset.id;
    priceDes = priceStar[e.currentTarget.dataset.id - 1].des;
    for (var i = 0; i < priceStar.length; i++) {
      if (priceStar[i].id <= priceId) {
        priceStar[i].active = true;
      } else {
        priceStar[i].active = false;
      }
    }
    this.setData({
      priceStar: priceStar,
      priceId: priceId,
      priceDes: priceDes,
    })
  },
  // 
  serviceStarTap: function(e) {
    var serviceStar = this.data.serviceStar;
    var serviceId = this.data.serviceId;
    var serviceDes = this.data.serviceDes;
    serviceId = e.currentTarget.dataset.id;
    serviceDes = serviceStar[e.currentTarget.dataset.id - 1].des;
    for (var i = 0; i < serviceStar.length; i++) {
      if (serviceStar[i].id <= serviceId) {
        serviceStar[i].active = true;
      } else {
        serviceStar[i].active = false;
      }
    }
    this.setData({
      serviceStar: serviceStar,
      serviceId: serviceId,
      serviceDes: serviceDes,
    })
  },
  // 
  qualityStarTap: function(e) {
    var qualityStar = this.data.qualityStar;
    var qualityId = this.data.qualityId;
    var qualityDes = this.data.qualityDes;
    qualityId = e.currentTarget.dataset.id;
    qualityDes = qualityStar[e.currentTarget.dataset.id - 1].des;
    for (var i = 0; i < qualityStar.length; i++) {
      if (qualityStar[i].id <= qualityId) {
        qualityStar[i].active = true;
      } else {
        qualityStar[i].active = false;
      }
    }
    this.setData({
      qualityStar: qualityStar,
      qualityId: qualityId,
      qualityDes: qualityDes,
    })
  },
  // 
  content: function(e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 订单信息
  orderDetail: function() {
    var that = this;
    var uid = this.data.uid;
    var orderId = this.data.orderId;
    var orderInfo = this.data.orderInfo;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=OrderDetail&UID=" + uid + "&OrderID=" + orderId + "&type=0"),
      success: function(res) {
        orderInfo = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        that.setData({
          orderInfo: orderInfo,
          orderNumber: orderInfo.OrderNumber,
        })

      }
    })
  },
  // 
  evaluate: function() {
    var that = this;
    var uid = this.data.uid;
    var orderId = this.data.orderId;
    var orderNumber = this.data.orderNumber;
    var degreeId = this.data.degreeId;
    var priceId = this.data.priceId;
    var serviceId = this.data.serviceId;
    var qualityId = this.data.qualityId;
    var content = this.data.content;
    var imgList = this.data.imgList;
    var imgStr = imgList.join(";");
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=EvaluationGoods&UID=" + uid + "&OrderNumber=" + orderNumber + "&Evaluation=" + degreeId + "&PriceStar=" + priceId + "&ServiceStar=" + serviceId + "&QualityStar=" + qualityId + "&Describe=" + content + "&Images=" + imgStr),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 
})